//
//  AboutUsViewController.swift
//  Exchange
//
//  Created by rui on 2018/4/19.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit


class AboutUsViewController: BaseViewController, UITableViewDelegate, UITableViewDataSource{
    
    private let kSettingTableViewCellID = "kSettingTableViewCellID"
    
    lazy var viewModel: AboutUsViewModel = {
        let model = AboutUsViewModel(controller: self)
        return model
    }()
    
    lazy var tableViewHeaderView: UIView = {
        let headView = UIView()
        headView.backgroundColor = UIColor.red
        let icon = UIImageView(image: #imageLiteral(resourceName: "login_icon"))
        icon.centerX = kSCREEN_WIDTH * 0.5
        icon.y = 80
        headView.addSubview(icon)
        let version = UILabel()
        version.text = UIDevice.current.appVersion
        version.width = kSCREEN_WIDTH
        version.height = 20
        version.y = icon.bottom + 20
        version.textAlignment = .center
        version.themeTextColor = ThemeManager.ThemeColorKeys.text1
        version.font = UIFont.systemFont(ofSize: 14)
        headView.addSubview(version)
        headView.height = 250
        headView.themeBackground = ThemeManager.ThemeColorKeys.background
        return headView
    }()
    
    lazy var tableView: UITableView = {
        let table = UITableView(frame: self.view.bounds, style: .grouped)
        table.delegate = self
        table.dataSource = self
        table.register(SettingTableViewCell.self, forCellReuseIdentifier: kSettingTableViewCellID)
        table.tableHeaderView = tableViewHeaderView
        table.themeBackground = ThemeManager.ThemeColorKeys.margin
        table.themeSeparatorColor = ThemeManager.ThemeColorKeys.separator
        table.separatorInset = UIEdgeInsetsMake(0, 16, 0, 16)
        table.sectionHeaderHeight = 0.0001
        return table
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initData()
    }
    
    func initUI() {
        self.title = kText("关于我们")
        self.view.addSubview(tableView)
        let titleHeight = self.titleLabel?.frame.size.height ?? 0
        tableView.frame = CGRect(x: 0, y: titleHeight, width: self.view.frame.size.width, height: self.view.height - titleHeight)
    }
    
    func initData() {
        viewModel.loadVersionInfo()
    }
    
    // MARK: - UITableViewDelegate UITableViewDataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kSettingTableViewCellID, for: indexPath)
        cell.textLabel?.themeTextColor = ThemeManager.ThemeColorKeys.text1
        cell.detailTextLabel?.text = ""
        if indexPath.row == 0 {
            cell.textLabel?.text = kText("用户协议")
        }else {
            cell.textLabel?.text = kText("版本管理")
            cell.detailTextLabel?.text = viewModel.lastVersion
            if viewModel.isNewVersion {
                cell.detailTextLabel?.textColor = kRedColor
            }else {
                cell.detailTextLabel?.themeTextColor = ThemeManager.ThemeColorKeys.text2
            }
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        if indexPath.row == 0 {
            dPrint("用户协议界面")
        }else {
            ///跳转到下载地址
            self.updateApp()
        }
    }
    
    func updateApp() {
        viewModel.updateApp()
    }
}





